<?php

require_once("tournaments_dao.php");

class brackets_dao{
	
	static function getBracket($tournament){
		$file = "dao/brackets.xml";
		
		$xml = simplexml_load_file($file);
		$brackets = $xml->bracket;
		foreach($brackets as $bracket){
			if ((string)$bracket->tournament === $tournament){
				return $bracket;
			}
		}
		return null;
	}
	
	static function generateBracket($tournament){
		if (!is_null(brackets_dao::getBracket($tournament))){
			return false;
		}
		
		//Forandrer status paa turneringen til "Startet"
		
		$file = "dao/tournaments.xml";
		$xml = simplexml_load_file($file);
		
		foreach($xml as $manytour) {
			if((string)$manytour->name === (string)$tournament->name) {
				$manytour->status = "Started";
			}
		}
		$xml->asXML($file);
		
				
		$file = "dao/brackets.xml";
		$xml = simplexml_load_file($file);
		
		$bracket = $xml->addChild('bracket');
		$bracket->addChild('tournament',$tournament->name);
		
		$nb_players = $tournament->numberofplayers;
		
		$obj = (array)$tournament->users;
		$allusers = $obj['user'];
		shuffle($allusers);
		
		$tomme_plasser = $nb_players - (count($allusers));
		
		while(($nb_players/2) <= $tomme_plasser) {
			$nb_players = $nb_players/2;
			$tomme_plasser = $nb_players - (count($allusers));
		}
		
		$hopp = floor($nb_players / $tomme_plasser);
		
		for ($i=0, $j = -1; $i<$tomme_plasser; $i++){
			$j += $hopp;
			//echo "j: " . $j;
			$allusers[] = $allusers[$j];
			$allusers[$j] = "-";
		}
		
		//exit();
		
		$teller = 0;
		$idSetter = 1;
		for ($i = 0; $i < log($nb_players, 2); $i++){ //Runde
			$idSetter = 1;
			$round = $bracket->addChild('round'); 
			for ($j = 0; $j<($nb_players/pow(2,($i+1))); $j++){ //match
				$match = $round->addChild('match');
				$match->addChild('id',($idSetter++));
				$match->addChild('status','Not started');
				if($i==0) {
					for ($k=0; $k<2; $k++){ //userinfo..
						$user = $match->addChild('user');
						$user->addChild('winner', 'false');
						$user->addChild('score', 0);
						$tempuser = $user->addChild('name',$allusers[$teller++]);
					}
				}
			}
		}
		
		brackets_dao::addAutoWin($bracket);
		$xml->asXML($file);
	}
	
	function reportLoss($yourname, $tournamentname, $yourscore, $hisscore, $inputround) {

		if((!(tournaments_dao::hasLost($yourname,$tournamentname))) && ($yourname != "-")) {
			echo "Du har allerede tapt..";
			exit();
		}
		
		$file = "dao/brackets.xml";
		$xml = simplexml_load_file($file);
		$rbracket = null;
		$brackets = $xml->bracket;
		foreach($brackets as $bracket){ //Finne riktig turnering
			if ((string)$bracket->tournament == $tournamentname){
				$rbracket = $bracket;
			}
		}
		
		/*** Fikser ting i naavarende runde ***/
		
		$round = $rbracket->round[$inputround];		
		$id = null;
		foreach($round as $match) {
			
			if($match->user[0]->name == $yourname || 
			$match->user[1]->name == $yourname) {
				
				$id = $match->id;
				if($match->user[0]->name == $yourname) { 
					$match->user[0]->score = $yourscore;
					$match->user[1]->score = $hisscore;
					$match->user[1]->winner = "true";
					$winner = $match->user[1];
					$match->status = "Finished";
				}
				else { 
					$match->user[1]->score = $yourscore;
					$match->user[0]->winner = "true";
					$match->user[0]->score = $hisscore;
					$winner = $match->user[0];
					$match->status = "Finished";
				}	
			}
		}
		
		/*** Fikser ting i neste runde ***/ 
		$round2 = $rbracket->round[$inputround+1];
		
		if(1 == ($id % 2)) { //Trikset for aa fikse riktig
			$id = $id+1; //kampoppsett.
		}
		
		foreach($round2 as $match) {
			
			if(($id/2) == $match->id) {
				$user = $match->addChild('user');
				$user->addChild('winner', 'false');
				$user->addChild('score', 0);
				$user->addChild('name',$winner->name);
				break;
			}
		}
		//print_r($xml);
		return $xml->asXML($file);
		exit();
	}
function findRound($name, $tournamentname) {
	$file = "dao/brackets.xml";
	$xml = simplexml_load_file($file);
	
	$rbracket = null;
	$brackets = $xml->bracket;
	foreach($brackets as $bracket){ //Finne riktig turnering
		if ((string)$bracket->tournament === $tournamentname){
			$rbracket = $bracket;
		}
	}
	$last_found_round = 0;
		
	$round = $rbracket->round;
	$roundcounter = 0;
		
	foreach($round as $match) {
		foreach($match as $users) {
			foreach($users as $user)				
				if ($user->name == $name) {
					$last_found_round = $roundcounter; 
				}
			}
			$roundcounter++;
		}
		return $last_found_round;		
	}
	
	function addAutoWin($bracket) {		
		$rundeteller = 0;
		
		foreach($bracket->round as $round) {
			
			if($rundeteller == 0) {	//Runde ein.
				foreach($round->match as $match) {
					if($match->user[0]->name == "-"){
						$match->user[0]->score = 0;
						$match->user[1]->winner = "true";
						$match->user[1]->score = 1;
						$match->status = "Finished";
						$winners[] = (string)$match->user[1]->name;
						$winnersid[] = (int)$match->id;
					 }
					 else if($match->user[1]->name == "-"){
					 	$match->user[1]->score = 0;
						$match->user[0]->winner = "true";
						$match->user[0]->score = 1;
						$match->status = "Finished";
						$winners[] = (string)$match->user[0]->name;
						$winnersid[] = (int)$match->id;
					 }
					else {
						//To spillere..
					}
				}
			}
			
			
			
			//Fikse ting for runde 2.
			if($rundeteller == 1) {
				foreach($winnersid as $key => $winnerid) {
					if(1 == ($winnerid % 2)) { //Trikset for aa fikse riktig
						$winnersid[$key] = $winnerid+1; //kampoppsett.
					}
				}
				
				echo "<pre>";
				print_r($winnersid);
				
				$matches = $bracket->round[1];
				foreach($matches as $match) {					
					foreach($winnersid as $key => $winnerid) {						
						if(($winnerid/2) == $match->id) {
							$user = $match->addChild('user');
							$user->addChild('winner', 'false');
							$user->addChild('score', 0);
							$user->addChild('name',$winners[$key]);
							}
						}
					}
					
				}
			$rundeteller++;
		}
	}
	
	function hasOpponent($inputuser, $tournament) {
		
		$roundnumber = brackets_dao::findRound($inputuser,$tournament);
		
		$file = "dao/brackets.xml";
		$xml = simplexml_load_file($file);
	
		$rbracket = null;
		$brackets = $xml->bracket;
		foreach($brackets as $bracket){ //Finne riktig turnering
			if ((string)$bracket->tournament === $tournament){
				$rbracket = $bracket;
			}
		}
		
		$round = $rbracket->round[$roundnumber];
		foreach($round->match as $match) {
			foreach($match->user as $user) {				
				if((string)$user->name === $inputuser) {
					if(count($match->user) > 1) return true;
					else return false;
				}
			}
			
		}
		
		exit();
		
	}
	
}

?>
